package hw;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileData extends Bundle {
	private final String name;
	private final byte[] fileBytes;
	private final long lastModified;
	public FileData(final File f, final long lastMod) throws IOException {
		name=f.getName();
		final FileInputStream fis = new FileInputStream(f);
		final BufferedInputStream bis = new BufferedInputStream(fis);
		fileBytes=new byte[(int) f.length()];
		bis.read(fileBytes,0,fileBytes.length);
		bis.close();
		lastModified=lastMod;
	}
	public FileData(final File f) throws IOException {
		this(f, f.lastModified());
	}
	public String getName() {
		return name;
	}

	public void writeToFile(final File f) throws IOException {
		if (!f.exists()) {
			f.getParentFile().mkdirs();
			f.createNewFile();
		}
		final FileOutputStream fos = new FileOutputStream(f);
		final BufferedOutputStream bos = new BufferedOutputStream(fos);
		bos.write(fileBytes);
		bos.close();
	}
	@Override
	public long getLastModified() {
		return lastModified;
	}
	

}
